home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / demos / OpenGL / drip / Drip.c++ next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  4.9 KB  |  163 lines

  1. /*
  2.  * (c) Copyright 1993, 1994, Silicon Graphics, Inc.
  3.  * ALL RIGHTS RESERVED 
  4.  * Permission to use, copy, modify, and distribute this software for 
  5.  * any purpose and without fee is hereby granted, provided that the above
  6.  * copyright notice appear in all copies and that both the copyright notice
  7.  * and this permission notice appear in supporting documentation, and that 
  8.  * the name of Silicon Graphics, Inc. not be used in advertising
  9.  * or publicity pertaining to distribution of the software without specific,
  10.  * written prior permission. 
  11.  *
  12.  * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
  13.  * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
  14.  * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
  15.  * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
  16.  * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
  17.  * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
  18.  * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
  19.  * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
  20.  * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN
  21.  * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
  22.  * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
  23.  * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
  24.  * 
  25.  * US Government Users Restricted Rights 
  26.  * Use, duplication, or disclosure by the Government is subject to
  27.  * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
  28.  * (c)(1)(ii) of the Rights in Technical Data and Computer Software
  29.  * clause at DFARS 252.227-7013 and/or in similar or successor
  30.  * clauses in the FAR or the DOD or NASA FAR Supplement.
  31.  * Unpublished-- rights reserved under the copyright laws of the
  32.  * United States.  Contractor/manufacturer is Silicon Graphics,
  33.  * Inc., 2011 N.  Shoreline Blvd., Mountain View, CA 94039-7311.
  34.  *
  35.  * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
  36.  */
  37. extern "C" {
  38. #include <GL/glu.h>
  39. #include <GL/glx.h>
  40. #include <malloc.h>
  41. #include <math.h>
  42. #include <stdlib.h>
  43. }
  44.  
  45. #include "Drip.h"
  46.  
  47. Drip::Drip() 
  48. {
  49.   outer_color[0] = ring_color[0] = inner_color[0] = 0.0;
  50.   outer_color[1] = ring_color[1] = inner_color[1] = 0.0;
  51.   outer_color[2] = ring_color[2] = inner_color[2] = 0.0;
  52.   outer_color[3] = inner_color[3] = 0.0;
  53.   ring_color[3] = 1.0;
  54.  
  55.   outer_radius = 1.0;
  56.   ring_radius = outer_radius / 2.0;
  57.  
  58.   divisions = 50;
  59.   points = NULL;
  60.   
  61. }
  62.  
  63. void Drip::draw() 
  64. {
  65.   int i;
  66.  
  67.   glBegin(GL_TRIANGLES);
  68.   for (i = 0; i < divisions; i++) {
  69.     glColor4fv(inner_color);
  70.     glVertex2f(0.0, 0.0);
  71.     glColor4fv(ring_color);
  72.     glVertex2f(points[2*i] * ring_radius, points[2*i + 1] * ring_radius);
  73.     glVertex2f(points[2*((i+1) % divisions)] * ring_radius,
  74.            points[(2*((i+1) % divisions)) + 1] * ring_radius);
  75.   }
  76.   glEnd();
  77.  
  78.   glBegin(GL_QUADS);
  79.   for (i = 0; i < divisions; i++) {
  80.     glColor4fv(ring_color);
  81.     glVertex2f(points[2*i] * ring_radius, points[2*i + 1] * ring_radius);
  82.     glVertex2f(points[2*((i+1) % divisions)] * ring_radius,
  83.            points[(2*((i+1) % divisions)) + 1] * ring_radius);
  84.     glColor4fv(outer_color);
  85.     glVertex2f(points[2*((i+1) % divisions)] * outer_radius,
  86.            points[(2*((i+1) % divisions)) + 1] * outer_radius);
  87.     glVertex2f(points[2*i] * outer_radius, points[2*i + 1] * outer_radius);
  88.    }
  89.   glEnd();
  90.  
  91. }
  92.  
  93. void Drip::draw_starburst() 
  94. {
  95.   int i;
  96.  
  97.   glBegin(GL_TRIANGLES);
  98.   for (i = 0; i < divisions; i++) {
  99.     glColor4fv(inner_color);
  100.     glVertex2f(0.0, 0.0);
  101.     glColor4fv(ring_color);
  102.     glVertex2f(points[2*i] * ring_radius, points[2*i + 1] * ring_radius);
  103.     glVertex2f(points[2*((i+1) % divisions)] * ring_radius,
  104.            points[(2*((i+1) % divisions)) + 1] * ring_radius);
  105.   }
  106.   glEnd();
  107.  
  108.   glBegin(GL_QUADS);
  109.   for (i = 0; i < divisions; i++) {
  110.     glColor4fv(ring_color);
  111.     glVertex2f(points[2*i] * ring_radius, points[2*i + 1] * ring_radius);
  112.     glVertex2f(points[2*((i+1) % divisions)] * ring_radius,
  113.            points[(2*((i+1) % divisions)) + 1] * ring_radius);
  114.     glColor4fv(outer_color);
  115.     glVertex2f(points[2*i] * outer_radius, points[2*i + 1] * outer_radius);
  116.     glVertex2f(points[2*((i+1) % divisions)] * outer_radius,
  117.            points[(2*((i+1) % divisions)) + 1] * outer_radius);
  118.   }
  119.   glEnd();
  120.  
  121. }
  122.  
  123.  
  124. void Drip::set_divisions(int new_divisions) 
  125. {
  126.   divisions = new_divisions;
  127. }
  128.  
  129. int Drip::get_divisions() {
  130.   return divisions;
  131. }
  132.  
  133. void Drip::set_points(float *new_points) {
  134.   points = new_points;
  135. }
  136.  
  137. void Drip::alloc_points() {
  138.   points = (float *)malloc(divisions * 2 * sizeof(*points));
  139.   if (divisions > 0 && points == NULL) {
  140.     fprintf(stderr, "Malloc failed.\n");
  141.     exit(1);
  142.   }
  143. }
  144.                     
  145.  
  146. void Drip::fill_points() {
  147.   int i;
  148.   float theta;
  149.   float delta;
  150.  
  151.   delta = 2.0 * M_PI / (float)divisions;
  152.   theta = 0.0;
  153.   for (i = 0; i < divisions; i++) {
  154.     points[2 * i] = cos(theta);
  155.     points[2 * i + 1] = sin(theta);
  156.     theta += delta;
  157.   }
  158. }
  159.  
  160. float *Drip::get_points() {
  161.   return points;
  162. }
  163.